package com.yonyou.crm.sys.menu.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yonyou.crm.common.login.context.LoginContextConstant;
import com.yonyou.crm.sys.role.constant.RoleTypeConstant;
import com.yonyou.crm.sys.role.entity.FuncresourceVO;
import com.yonyou.crm.sys.role.entity.RoleVO;
import com.yonyou.crm.sys.role.service.FunctionBizServiceImpl;
import com.yonyou.crm.sys.role.service.RoleBizServiceImpl;
import com.yonyou.crm.sys.user.entity.TypeEnum;
import com.yonyou.crm.sys.user.entity.UserVO;
import com.yonyou.crm.sys.user.service.UserBizServiceImpl;
import com.yonyou.iuap.context.InvocationInfoProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.model.util.ModelUtil;
import com.yonyou.crm.sys.menu.entity.MenuVO;
import com.yonyou.crm.sys.menu.entity.MenuDO;

@Service
public class MenuBizServiceImpl extends BaseBizServiceImpl<MenuDO>{

	@Autowired
	private UserBizServiceImpl userService;
	@Autowired
	private FunctionBizServiceImpl funcService;
	@Autowired
	private RoleBizServiceImpl roleService;


	public List<MenuVO> getMenuList(Long menuGroupId) {
		Map<String, Object> param = new HashMap<>();

		//********************************************************************************
		List<MenuDO> returnMenus = new ArrayList<>();
		String userId = InvocationInfoProxy.getParameter(LoginContextConstant.UserId);
		UserVO userVO = userService.getDetail(Long.parseLong(userId));
		//根据 用户 获取 对应的角色
		Integer userType = userVO.getType();
		List<RoleVO> roleList = roleService.getUserRole(userVO.getId());


		//1.根据用户类型 来过滤菜单，哪些菜单 管理员可见，哪些 菜单普通人员可见
		param.put("userType",userType);

		if(roleList != null && roleList.size() > 0) {
			RoleVO role = roleList.get(0);
			//2.根据当前登录用户的角色 获取已经配置好的功能资源
			List<Long> hasFuncMenuList = new ArrayList<>();
			List<FuncresourceVO> funcResources = funcService.getFuncResourceList(role.getId());
			if(funcResources != null && funcResources.size() > 0) {
				for(FuncresourceVO resource : funcResources) {
					Long menuId = resource.getMenuId();
					if(menuId != null && !menuId.equals(0L)) {
						//获取 功能资源中的菜单资源  一般是xx浏览 等
						hasFuncMenuList.add(menuId);
					}
				}
			}

			if(RoleTypeConstant.GROUPOFFICE.equals(role.getType()+"")) {
				//如果登录用户的角色是 集团内勤 可以看到集团客户这个菜单节点
				hasFuncMenuList.add(27L);
				hasFuncMenuList.add(28L);
				//集团内勤 不可见 客户地图 和 客户状态
				if(hasFuncMenuList.contains(30L)) {
					hasFuncMenuList.remove(30L);
				}
				if(hasFuncMenuList.contains(31L)) {
					hasFuncMenuList.remove(31L);
				}
			}
			if(hasFuncMenuList.size() > 0){
				param.put("hasFuncMenuList", hasFuncMenuList.toArray(new Long[0]));
			}
		}


		//********************************************************************************

		param.put("menuGroupId", menuGroupId);
		List<MenuDO> menus =dao.selectVoList(MenuDO.class, "getMenuList",param);


		if(menus!=null&&menus.size()>0){
			List<MenuVO> menuvos = new ArrayList<MenuVO>(menus.size()); 
			for(MenuDO menuDo : menus){
				MenuVO vo = ModelUtil.copyModel(menuDo, MenuVO.class);
				menuvos.add(vo);
			}
			return menuvos;
		}
		return null;
	}

}
